1778D - Flexible String Revisit - CodeForces Solution


combinatorics dp math probabilities

Please click on ads to support us..

C++ Code:

//f[i] = a[i]+b[i]*f[i+]
#include <bits/stdc++.h>
using namespace std;

#define int long long
const int M=998244353, N=1001000; 

int mmul(int i, int t){
  if(t==0)return 1;
  int res=mmul(i, t/2); 
  res=res*res%M;
  if(t&1)res=res*i%M;
  return res;}
int inv(int i){return mmul(i, M-2);}

int a[N], b[N], f[N];

void solve(int n){
  f[0]=0, a[1]=1, b[1]=(n-1)*inv(n)%M;
  for(int i=2; i<n; i++){
    int z= 1 - i*inv(n)%M*b[i-1]%M;
    a[i] = i*inv(n)%M*a[i-1]%M +1;
    a[i]=a[i]%M*inv(z)%M;
    b[i]=(n-i)*inv(n)%M*inv(z)%M;
  }
  f[n]=(a[n-1]+1)*inv(1-b[n-1])%M;
  for(int i=n-1; i>=1; i--)f[i]=(a[i]+b[i]*f[i+1])%M;
return;}

signed main(){
  int t;
  cin >> t;
  while(t--){
    string a, b;
    int cnt=0, n;
    cin >> n >> a >> b;
    for(int i=0; i<n; i++)if(a[i]!=b[i])cnt++;
    solve(n);
    cout << (f[cnt]%M+M)%M << '\n';
  }
return 0;}


Comments

Submit
0 Comments
More Questions

1367B - Even Array
136A - Presents
1450A - Avoid Trygub
327A - Flipping Game
411A - Password Check
1520C - Not Adjacent Matrix
1538B - Friends and Candies
580A - Kefa and First Steps
1038B - Non-Coprime Partition
43A - Football
50A - Domino piling
479A - Expression
1480A - Yet Another String Game
1216C - White Sheet
1648A - Weird Sum
427A - Police Recruits
535A - Tavas and Nafas
581A - Vasya the Hipster
1537B - Bad Boy
1406B - Maximum Product
507B - Amr and Pins
379A - New Year Candles
1154A - Restoring Three Numbers
750A - New Year and Hurry
705A - Hulk
492B - Vanya and Lanterns
1374C - Move Brackets
1476A - K-divisible Sum
1333A - Little Artem
432D - Prefixes and Suffixes